<?php
include "connect/connect_dsccmis.php";
mysql_query("set names tis620");
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
//XML Header
header("content-type:text/xml");

createKmlFile();

function createKmlFile(){
 // Selects all the rows in the markers table.
 $cond_date = "";
 $chkuse_date = $_GET["chkuse_date"];
 $date_start = $_GET["date_start"];
 $date_end = $_GET["date_end"];
 if($chkuse_date=="1"){
 	$cond_date = " and (evndte >= '".$date_start."' and evndte <= '".$date_end."')"; 
 }
 
 $cond_prov = "";
 $chkuse_prov = $_GET["chkuse_prov"];
 if($chkuse_prov=="1"){
 	$chkbox90 = $_GET["chkbox90"];
	$chkbox94 = $_GET["chkbox94"];
	$chkbox95 = $_GET["chkbox95"];
	$chkbox96 = $_GET["chkbox96"];
	if($chkbox90=="90") $cond_prov=$cond_prov.",'90'";
	if($chkbox94=="94") $cond_prov=$cond_prov.",'94'";
	if($chkbox95=="95") $cond_prov=$cond_prov.",'95'";
	if($chkbox96=="96") $cond_prov=$cond_prov.",'96'";
	$cond_prov=substr($cond_prov,1);
	$cond_prov=" and prov in (".$cond_prov.")";
 }

// event cause
 $cond_cause = $_GET["cond_cause"];
if($cond_cause!=""){
	$cond_cause = " and cdscc in ".$cond_cause;
}
// event type
 $cond_evntyp = $_GET["cond_evntyp"];
if($cond_evntyp!=""){
	$cond_evntyp = " and mis_evntypid in ".$cond_evntyp;
}

 $date_start = $_GET["date_start"];
 $date_end = $_GET["date_end"];


 $query = "select evnid,evnnme,evndte,evntme,lat,lng,mis_evntypid,plcnmegen from gis_event where lat is not null and lng is not null ".$cond_date.$cond_prov.$cond_cause.$cond_evntyp;
 
 //echo $query;

 $result = mysql_query($query);
 if (!$result) 
 {
  die('Invalid query: ' . mysql_error());
 }
 header('Content-type: application/vnd.google-earth.kml+xml');
//$File = "event".$y."_evntyp".$evntypid.".kml"; 
 $fname = $_GET["fname"];
$File = "kml/".$fname;
//$File = $fname;
if (!file_exists($File)) {//exist file not need to create file but if not will create
$Handle = fopen($File, 'w');
// Creates an array of strings to hold the lines of the KML file.
$temp=$kml = array('<?xml version="1.0" encoding="UTF-8"?>');
fwrite($Handle,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 
$temp=$kml[] = '<kml xmlns="http://earth.google.com/kml/2.1">';
fwrite($Handle, $temp."\n"); 
$temp=$kml[] = ' <Document>';
fwrite($Handle, $temp."\n"); 
// Iterates through the rows, printing a node for each row.
while ($row = @mysql_fetch_assoc($result)) 
{
  $temp = $kml[] = '<Placemark>';
  fwrite($Handle, $temp); 
  $temp = $kml[] = '<Point>';
  fwrite($Handle, $temp."\n"); 
  $temp = $kml[] = '<coordinates>' . $row['lng'] . ','  . $row['lat'] . '</coordinates>';
  fwrite($Handle, $temp."\n"); 
  $temp = $kml[] = '</Point>';
  fwrite($Handle, $temp."\n"); 
  /*$evntyp=$row['mis_evntypid'];
  if($evntyp=="1"){
  	$icon_image = "iconfile/Bom20Icon_new.png";
	$icon_size = "15";
  }else{
    if($evntyp=="2"){
	  $icon_image = "iconfile/gun2.png";
	  $icon_size = "30";
	}else{
	    if($evntyp=="3"){
	  		$icon_image = "iconfile/fire-icon.png";
			$icon_size = "20";
		}else{
			$icon_image = "iconfile/circle_red.png";
			$icon_size = "15";
		}
	}
  }
  */
  //$temp = $kml[] = '<name>'. substr($row['evnnme'],0,40).'|'.$icon_image.'|'.$icon_size.'</name>';
  $temp = $kml[] = '<name>'. substr($row['evnnme'],0,40).'</name>';
  fwrite($Handle, iconv('TIS-620', 'UTF-8', $temp."\n")); 
  //$temp = $kml[] = '<Icon>fire-icon.png</iconpic>';
  //fwrite($Handle, $temp."\n"); 
  $evndte=(int)substr($row['evndte'],0,4)-543;
  $temp = $kml[] = '<TimeStamp><when>'.$evndte.'-'.substr($row['evndte'],4,2).'-'.substr($row['evndte'],6,2).'</when></TimeStamp>';
  fwrite($Handle, $temp."\n"); 
$user_type = $_GET["user_type"];
 if(($user_type=="2") || ($user_type=="3") || ($user_type=="4") || ($user_type=="5")){
	  $temp = $kml[] = '<description>&lt;br&gt;Date '.substr($row['evndte'],6,2).'-'.substr($row['evndte'],4,2).'-'.substr($row['evndte'],0,4).'   '.substr($row['evntme'],0,2).':'.substr($row['evntme'],2,2).' .&lt;br&gt;'.$row['plcnmegen'].'&lt;br&gt; &lt;a href=&quot;#&quot; onclick=&quot;javascript:popUp(\'show_event_des.php?evnid='.$row['evnid'].'\',\'400\',\'300\');&quot;&gt;...More Info..&lt;/a&gt;</description>';
 }else{
	   $temp = $kml[] = '<description>&lt;br&gt;Date '.substr($row['evndte'],6,2).'-'.substr($row['evndte'],4,2).'-'.substr($row['evndte'],0,4).'   '.substr($row['evntme'],0,2).':'.substr($row['evntme'],2,2).' .&lt;br&gt;'.$row['plcnmegen'].'</description>';
 }
  fwrite($Handle, iconv('TIS-620', 'UTF-8', $temp."\n")); 
  //fwrite($Handle, $temp."\n");
  //$temp = $kml[] = "<Icon><href>iconfile/fire-icon.png</href></Icon>";
  //fwrite($Handle, $temp."\n"); 
  
 // $temp = $kml[] = "<IconStyle id=\"mystyle\">";
 //  fwrite($Handle, $temp."\n"); 
 /* $temp = $kml[] = "<Icon>";
    fwrite($Handle, $temp."\n"); 
  $temp = $kml[] = "<href>http://localhost/dsccmis/iconfile/test.ico</href>";
    fwrite($Handle, $temp."\n"); 
  $temp = $kml[] = "<scale>1.0</scale>";
    fwrite($Handle, $temp."\n"); 
  $temp = $kml[] = "</Icon>";
    fwrite($Handle, $temp."\n"); */
  //$temp = $kml[] = "</IconStyle>";
  //  fwrite($Handle, $temp."\n"); 
	
  $temp = $kml[] = '</Placemark>';
  fwrite($Handle, $temp."\n"); 
} 
// End XML file
$temp = $kml[] = ' </Document>';
fwrite($Handle, $temp."\n"); 
$temp = $kml[] = '</kml>';
fwrite($Handle, $temp."\n"); 
fclose($Handle); 
} //end if of line 71
}//end function
//$kmlOutput = join("\n", $kml);
//echo $kmlOutput;
//fclose($handle); 
?>
